package com.ny.controller;


import com.ny.entity.Classrooms;
import com.ny.entity.Reservations;
import com.ny.entity.Users;
import com.ny.service.ClassroomService;
import com.ny.service.ReservationService;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private ReservationService reservationService;

    //用户登入查询所以教室
    @RequestMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, Model model, HttpSession session) {
        Users user = userService.login(username, password);
        session.setAttribute("user", user);
        if (user!= null) {
            System.out.println(user);
            List<Classrooms> classroomsList = classroomService.findAllClassrooms();
            System.out.println(classroomsList);
            model.addAttribute("classroomList", classroomsList);
//            model.addAttribute("user", user);
            return "classroom";
        } else {
            model.addAttribute("error", "用户名或密码错误");
            return "login";
        }
    }


//多条件查询教室
    // 根据请求参数搜索教室
    @GetMapping("/searchClassrooms")
    public String searchClassrooms(
            // 教室名称
            @RequestParam(required = false) String classroomName,
            // 容量
            @RequestParam(required = false) String capacity,
            // 状态
            @RequestParam(required = false) String status,
            // 地点
            @RequestParam(required = false) String building,
            // 描述
            @RequestParam(required = false) String description,
            Model model) {
        // 调用服务层搜索教室
        List<Classrooms> classroomList = classroomService.searchClassrooms(classroomName, capacity, status, building, description);
        // 打印获取到的数据
        System.out.println("获取到的数据"+classroomList);
        // 将搜索结果添加到模型中
        model.addAttribute("classroomList", classroomList);
        // 返回教室页面
        return "classroom";
    }


    //查看用户预约记录
    @RequestMapping(value = "/myReservations", method = RequestMethod.GET)
    public String myReservations(Model model, HttpSession session) {
        Users user = (Users) session.getAttribute("user");
        if (user != null) {
            List<Reservations> reservationsList = reservationService.findAllReservations();
            model.addAttribute("reservationsList", reservationsList);
            return "myReservations";
        } else {
            model.addAttribute("error", "请先登录");
            return "redirect:/login";
        }
    }


    //进入个人中心
    @RequestMapping(value = "/personal_center", method = RequestMethod.GET)
    public String personal_center(Model model, HttpSession session) {
        Users user = (Users) session.getAttribute("user");
        System.out.println(user);
        if (user != null) {
            model.addAttribute("user", user);
            return "individualistic";
        } else {
            return "redirect:/login";
        }
    }

}
